package com.hs.readledkx.tutorial.client;

import onbon.bx06.*;
import onbon.bx06.area.TextCaptionBxArea;
import onbon.bx06.area.page.TextBxPage;
import onbon.bx06.file.BxFileWriterListener;
import onbon.bx06.file.ProgramBxFile;
import onbon.bx06.series.Bx6E;
import onbon.bx06.utils.TextBinary;

import java.awt.*;
import java.util.List;

public class WriteProgramTextCaption implements BxFileWriterListener<Bx6GScreen> {

    public static void send(String ip,int port,String content,int x,int y,int w,int h,int fsize) throws Exception {
        Bx6GEnv.initial();
        Bx6GScreenClient screen = new Bx6GScreenClient("MyScreen",new Bx6E());
        if (!screen.connect(ip, port)) {
            System.out.println("connect failed");
        }
        //
        ProgramBxFile program = new ProgramBxFile("P000", screen.getProfile());

        Bx6GScreenProfile profile = screen.getProfile();
        System.out.println("screen width1111111111 : " + profile.getWidth());
        System.out.println("screen height111111111 : " + profile.getHeight());
        System.out.println("x：" + x );
        System.out.println("y：" + y );
        System.out.println("w：" + w );
        System.out.println("h：" + h );
        System.out.println("screen height111111111 : " + profile.getHeight());
        System.out.println("screen height111111111 : " + profile.getHeight());
        //
        TextCaptionBxArea area = new TextCaptionBxArea(x, y, w, h, screen.getProfile());

//        area.loadFrameImage(1);
//        area.setFrameSpeed(1);
        TextBxPage page = new TextBxPage(content);
//        style
        // 设置文本水平对齐方式
        page.setHorizontalAlignment(TextBinary.Alignment.NEAR);
        // 设置文本垂直居中方式
        page.setVerticalAlignment(TextBinary.Alignment.CENTER);
        // 设置文本字体
        if(fsize != 0){
            page.setFont(new Font("宋体", Font.PLAIN, fsize));         // 字体
        }
        System.out.println(page.getFont());
        System.out.println("getFontName:" + page.getFont().getFontName());
        System.out.println("getFontsize:" + page.getFont().getSize());

        // 设置文本颜色
        page.setForeground(Color.red);
        // 设置区域背景色，默认为黑色
//        page.setBackground(Color.darkGray);
        //
        area.addPage(page);
        program.addArea(area);
        //
        screen.writeProgramAsync(program, new WriteProgramTextCaption());
//        Thread.sleep(5000);
        //
//        List<String> pgs = screen.readProgramList();
//        for (String pg : pgs) {
//            System.out.println(pg);
//        }
        //
//        screen.disconnect();
    }

    @Override
    public void cancel(Bx6GScreen owner, String fileName, Bx6GException ex) {
        ex.printStackTrace();
    }

    @Override
    public void done(Bx6GScreen owner) {

    }

    @Override
    public void fileFinish(Bx6GScreen owner, String fileName, int total) {
        System.out.println(fileName + " fileFinish:" + total);
    }

    @Override
    public void fileWriting(Bx6GScreen owner, String fileName, int total) {
        System.out.println(fileName + " fileWriting:" + total);
    }

    @Override
    public void progressChanged(Bx6GScreen owner, String fileName, int write, int total) {
        System.out.println(fileName + " progressChanged:" + write + "/" + total);
    }
}
